<template>
  <div>
    <el-card>
      <el-page-header icon="" title="成绩管理">
        <template #content>
          <span class="text-large font-600 mr-3">发布成绩</span>
        </template>
      </el-page-header>

      <el-table :data="tableData" style="width: 100%" stripe v-loading="loading">
        <el-table-column prop="courseid" label="课程编号" />
        <el-table-column prop="coursename" label="课程名称" />
        <el-table-column prop="major" label="专业" />
        <el-table-column prop="term" label="学期">
          <template #default="scope">
            {{ scope.row.term === 20231 ? '2023-2024学年第1学期' : '2023-2024学年第2学期' }}
          </template>
        </el-table-column>
        <el-table-column prop="credit" label="学分" />
        <el-table-column prop="name" label="任课教师" />
        <el-table-column prop="ispublish" label="是否发布">
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.ispublish">是</el-tag>
            <el-tag type="danger" v-else>否</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="220">
          <template #default="scope">
            <el-button size="default" @click="handleEdit(scope.row)" v-if="scope.row.ispublish === 0">录入成绩</el-button>
            <div v-else-if="scope.row.ispublish === 1">
              <el-button size="default" @click="handleReEdit(scope.row)" type="primary">录入补考</el-button>
              <el-button size="default" @click="handleBack(scope.row)" type="danger" v-if="store.state.userInfo.role === 1">撤销</el-button>
              <el-button size="default" @click="handleMailBack(scope.row)" type="warning" v-if="store.state.userInfo.role === 2">申请撤销</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'
const tableData = ref([])
const loading = ref(true)

const store = useStore()
const router = useRouter()
onMounted(() => {
  getTableData()
  // console.log(tableData)
})
const getTableData = async () => {
  if (store.state.userInfo.role === 1) {
    const res1 = await axios.get('/api/course/list')
    // console.log(res1)
    const res2 = await axios.get('/api/user/getteacherlist')
    // 将教师工号改为名字
    res1.data.data.forEach((item) => {
      res2.data.data.forEach((i) => {
        if (item['courseteacher'] === i['username']) {
          item['name'] = i['name']
        }
      })
    })
    tableData.value = res1.data.data
  } else if (store.state.userInfo.role === 2) {
    const res1 = await axios.get(`/api/course/mylist/${store.state.userInfo.username}`)

    const res2 = await axios.get('/api/user/getteacherlist')
    // 将教师工号改为名字
    res1.data.data.forEach((item) => {
      res2.data.data.forEach((i) => {
        if (item['courseteacher'] === i['username']) {
          item['name'] = i['name']
        }
      })
    })
    tableData.value = res1.data.data
  }
  loading.value = false
}
const handleEdit = (item) => {
  router.push(`/score-manage/editscore/${item._id}`)
}
const handleBack = async (item) => {
  // console.log(item)
  await axios.put(`/api/course/ispublish/${item.courseid}`, { ispublish: 0 })

  // 成绩表中的ispublish
  const res = await axios.get(`/api/score/list/${item.courseid}`)
  // console.log(res.data.data)
  res.data.data.forEach(async (i) => {
    i.ispublish = 0
    i.reexam = 0
    await axios.put(`/api/score/list/${i.courseid}`, i)
  })

  ElMessage.success('撤销成功！')
  getTableData()
}
const handleReEdit = (item) => {
  router.push(`/score-manage/reeditscore/${item._id}`)
}
const handleMailBack = async (item) => {
  await axios.post('/api/mail/submit', {
    name: item.name,
    coursename: item.coursename,
    courseid: item.courseid
  })
}
</script>

<style lang="scss" scoped>
.el-table {
  margin-top: 50px;
}
</style>
